<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.registry.UserManager" %>
<%@ page import="org.biojava.services.das.dao.Role" %>
<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.dao.ProjectBean"%>
<%@page import="org.biojava.services.das.registry.ShowLinkCapability"%>
<%@page import="org.biojava.services.das.registry.CapabilityLinkBean"%>
<%@ page import="org.biojava.dasobert.das.Capabilities"%>
<%@ page import="org.biojava.dasobert.dasregistry.DasCoordinateSystem"%>
<%@page import="java.util.*" %>
<jsp:directive.page import="java.util.Date"/>
<jsp:directive.page import="org.biojava.services.das.registry.DasRegistrySql" />
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasSource" />
<jsp:directive.page import="org.springframework.beans.factory.BeanFactory"/>
<jsp:directive.page import="org.springframework.context.support.ClassPathXmlApplicationContext"/>
<jsp:useBean id="coord" class="dasregistry.coordBean" scope="page"/>


<% 

String projectID = request.getParameter("project_id");
int project_id = -1;
if (projectID != null)
	try {
		project_id = Integer.parseInt(projectID);
	} catch(Exception e){
		out.print(e.getMessage());
	}
ProjectBean project = coord.getProject(project_id);
if ( project == null ) {
	project = new ProjectBean();
}

   String title=  project.getName();
   String header = title;
%>



<%@ include file="pagehead.jsp" %>

<%

	
BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
DasRegistrySql dasregistry=(DasRegistrySql)ctx.getBean("registryBean");
	
	List<DasSource> userSources    = dasregistry.getServicesForEmail(email);
	DasSource[] projectSources = coord.getDasSourcesForProject(project);
	
	boolean userOwnsOneSource = coord.userOwnsASource(user,projectSources);
	int invalidSources=0;
	

%>


    
<%  if ( email != null ) {%><script type="text/javascript">
<!--	
function checkUncheckAll(flag) {
     var theForm = document.getElementById('dassourceaction');
     var z = 0;
	 for(z=0; z<theForm.length;z++){
      	if(theForm[z].type == 'checkbox' ){
	  		theForm[z].checked = flag;
	  	}
	 }
}

function submitAction(object){
	var pos = object.options[object.selectedIndex].value;
	var theForm = document.getElementById('dassourceaction');
	if ( pos != 0 ) {
		theForm.submit();
	}
}
//--></script>
<%} %>
   <jsp:include page="openwindowscript.jsp" />
    
</head>

<body>
<%@ include file="pagebodystart.jsp" %>


    

<div id="main">
<h2>Details for project <%=project.getName() %></h2>



<%		
		RegistryUser owner = project.getOwner();
		if (owner == null)
			owner = new RegistryUser();
		
%>


<table>
		

<tr><td>icon:</td>
	<td><img src="ProjectIcon?id=<%=project.getProjectID()%>"/>
	</td>
	</tr>

<tr>
<td>Name:</td>				
<td><%=project.getName() %></td></tr>
	
<tr><td>homepage:</td>	
<td><a href="<%=project.getHomepage() %>"><%=project.getHomepage() %></a>
</td></tr>

<tr><td>owner:</td>
	<td><%=owner.getEmail() %></td></tr>

<tr><td>description</td>	
	<td><%=project.getDescription() %></td></tr>
	
<tr><td>Nr DAS sources:</td>	
	<td><a href="showProjectDetails.jsp?project_id=<%=project.getProjectID()%>"><%= coord.getNrDasSourcesForProject(project) %></a></td></tr>


</table>

<br/>



<%@ include file ="sangertablestart.jsp" %>
<form id="dassourceaction" name="dassourceaction" action="ProjectAction" method="POST" >
<input type="hidden" name="projectID" value="<%=project.getProjectID() %>"/>	
	
<%
if ( userOwnsOneSource ) {
	%>
	<tr class="row0top">
	
	<td colspan="4">Select: <a href="#" onClick="checkUncheckAll(true);">All</a>
	<a href="#" onClick="checkUncheckAll(false);">None</a>
	</td>
	
	<td align="right" colspan="3">action
		<select name="action" onChange="submitAction(this.form.action);">
		<option value="">select ...</option>	
		<option value="add">   add DAS sources</option>	
		<option value="remove">   remove from project</option>		
<% if (user.equals(owner)){ %>		
		<option value="edit">   edit project</option>		
<%} %>
		</select>
	</td>
	</tr>
	<tr class="row0bottom">
	
	 <%
} else {
%>
<tr class="row0" valign="top">
<%} %>

<td colspan="7">DAS sources for this project</td></tr>

<%
int twodays = 1000*60*60*24*2 ;
Date now = new Date();

	for (int i=0; i< projectSources.length;i++){
		DasSource ds = projectSources[i];
		boolean active = true;
		if (ds.getLeaseDate().getTime() < (now.getTime() - twodays)) {
			active = false;
		}
		%>
<% 	if (i % 2 != 0) {
%>
<tr class="row1" valign="top">
	<%
	} else {
	%>

<tr class="row2" valign="top">
<%} %>

<td>
<%
	for (int di=0; di< userSources.size(); di++) {
		DasSource userDS = userSources.get(di);
		if (userDS.equals(ds)){
			// this DAS source belongs to this user...
			%>
			<input type="checkbox" name="DS_ID" value="<%=ds.getId() %>"/>
			<% 
			
		}
	}

%>

</td>

<td><div title="header=[show details] body=[<%=ds.getDescription() %>]"><a href="showdetails.jsp?auto_id=<%=ds.getId()%>"> 
				<img src="img/messagebox_info16x16.png" border="0" alt="details"
				onMouseOver="window.status='show details'; return true;"
				onMouseOut="window.status=' '; return true;"/>
				</a></div></td>
<td><a href="showdetails.jsp?auto_id=<%=ds.getId()%>"><%=ds.getId() %></a></td>
<td>
<% 
if (!active) {
	invalidSources++;
	%> <div title="header=[server down?] body=[it was not possible to validate this server in > 2 days]"><a href="validateServer.jsp?auto_id=<%=ds.getId()%>"><img
		src="img/messagebox_warning.png"></a></div> <%
 			}
%>
</td>
<td><%=ds.getNickname() %></td>
<td nowrap>
	
			<%
			
			DasCoordinateSystem[] coordSys = ds.getCoordinateSystem();
			String testCode = "";

			boolean canFeatures = false;
			if (coordSys.length > 0) {
				testCode = coordSys[0].getTestCode();
			}

			String url = ds.getUrl();
			String nickname = ds.getNickname();
ShowLinkCapability linkCapab=new ShowLinkCapability();
		
CapabilityLinkBean[]links=linkCapab.showCapabilityLink(ds,testCode);
	//loop over all possible capabilities for a das server in the order we want as specified by the getCapabilitiesInCoreOrder
	for (int allCapsIndex = 0; allCapsIndex < links.length; allCapsIndex++) {
		CapabilityLinkBean bean=links[allCapsIndex];
		String beanCapability=bean.getCapability();
		String messageHeader=bean.getMessageHeader();
		String messageBody=bean.getMessageBody();
		String img=bean.getImg();
		String cmd=bean.getCmd();
		
		
			%>
			<span title="header=[<%=beanCapability%><%=messageHeader%>] body=[<%=beanCapability%> <%=messageBody%>]">
			<a href="javascript:openWindow('<%=cmd%>','',700,700)"><img src=<%=img %>></a> </span>
			<%
		
		
		
	}//end of looping over all capabilities
%>	
			
	</td>






<td><%=ds.getUrl() %></td>
</tr>
		<% 
	}

%>
<%@ include file ="sangertableend.jsp" %>
</form>
<font color="red" ><b>Number of broken sources =<%=invalidSources%>/<%=coord.getNrDasSourcesForProject(project)%></b></font>
<% if (email != null){
	%>
<br/>
<br/>
<form id="dassourceaction" name="dassourceaction" action="DasSourceAction" method="POST" >
<input type="hidden" name="projectID" value="<%=project.getProjectID()%>"/>
<input type="hidden" name="action" value="add"/>
<input type="submit" value="add DAS sources to this project"/>
</form>	
	<%
	
}
%>
</div>
<script src="boxover.js"></script>
<%@ include file="sangerfooter.jsp" %>


